// 获取指定用户信息
import express from 'express';
import pool from "../importantApi/db_connection.js";

const router = express.Router();
router.get('/', async (req, res) => {
    const {userId} = req.query;
    if (!userId) {
        return res.status(401).json({
            code: 401,
            msg: "缺少必要参数"
        })
    }
    let sql = `
        SELECT u.user_id,
               u.user_name,
               u.createtime,
               u.identity,
               MAX(CASE WHEN uc.class_id IS NOT NULL THEN 1 ELSE 0 END) AS hasJoinClass
        FROM userinfo u
                 LEFT JOIN user_class uc ON u.user_id = uc.user_id
        WHERE u.user_id = ${userId}
        GROUP BY u.user_id, u.identity;
    `
    let [result] = await pool.query(sql)
    if (result.length > 0) {
        res.status(200).json({
            code: 200,
            msg: "信息获取成功",
            data: result[0]
        })
    } else {
        res.status(201).json({
            code: 201,
            msg: "用户信息不存在"
        })
    }
})
export default router;